//
//  WeatherDetailsView.m
//  WeatherGuide
//
//  Created by lanou3g on 15/12/18.
//  Copyright © 2015年 李明. All rights reserved.
//

#import "WeatherDetailsView.h"
#import "LabelView.h"
#import "Weather.h"
#import "HourlyForecast.h"

@implementation WeatherDetailsView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self allViews];
    }
    return self;
}


- (void)allViews
{
    //天气详情
    self.detailsLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    self.detailsLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    self.detailsLabel.text = @"天气详情";
    self.detailsLabel.layer.borderWidth = 1;
    self.detailsLabel.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:self.detailsLabel];
    
    self.windView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 60, 60)];
    self.windView.image = [UIImage imageNamed:@"iconfont-fengche.png"];
    self.windView.layer.anchorPoint = CGPointMake(0.5, 0.5);
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    [self addSubview:self.windView];
    
    self.windSpeedLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, 100, 30)];
    self.windSpeedLabel.text = @"风速1km/h";
    self.windSpeedLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.windSpeedLabel];
    
    self.windDirAndScLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 160, 80, 30)];
    self.windDirAndScLabel.text = @"西南风 微风";
    self.windDirAndScLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.windDirAndScLabel];
    
    CGFloat wight = self.bounds.size.width - 150;
    self.flLabelView = [[LabelView alloc] initWithFrame:CGRectMake(140, 50, wight, 45)];
    
    self.flLabelView.leftLabel.text = @"体感温度";
    self.flLabelView.leftLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.flLabelView];
    
    self.hunLabelView = [[LabelView alloc] initWithFrame:CGRectMake(140, 95, wight, 45)];
    self.hunLabelView.leftLabel.text = @"温度";
    self.hunLabelView.leftLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.hunLabelView];
    
    self.visLabelView = [[LabelView alloc] initWithFrame:CGRectMake(140, 140, wight, 45)];
    self.visLabelView.leftLabel.text = @"能见度";
    self.visLabelView.leftLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.visLabelView];
    
}

- (void)timeAction{
    self.windView.layer.transform = CATransform3DRotate(self.windView.layer.transform, M_PI / 30, 0, 0, 1);
}

- (void)setWeather:(Weather *)weather
{
   
//    NSLog(@"%@",weather);
    NSMutableArray *array = [NSMutableArray array];
    NSTimeInterval tempTimeIndterval = 60 * 60 * 24;
    NSInteger index = 1;
    for (int i = 1; i < 6; i++) {
        HourlyForecast *hourlyForecast = [weather valueForKey:[NSString stringWithFormat:@"hourlyForecast%d",i]];
//        NSLog(@"%@",hourlyForecast.date);
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSDate *date = [formatter dateFromString:hourlyForecast.date];
        NSTimeInterval timeIndterval = [date timeIntervalSinceNow];
//        NSTimeInterval timeIndterval = [date timeIntervalSinceNow];
//            NSLog(@"%f,%f",timeIndterval,tempTimeIndterval);
            if (timeIndterval > 0 ) {
                if (timeIndterval < tempTimeIndterval) {
                    tempTimeIndterval = timeIndterval;
                    index = i;
                }
            }
    }
//    NSLog(@"index = %ld",index);
    HourlyForecast *hourlyForecast = [weather valueForKey:[NSString stringWithFormat:@"hourlyForecast%ld",index - 1]];
    if (hourlyForecast.date == nil) {
          hourlyForecast = [weather valueForKey:[NSString stringWithFormat:@"hourlyForecast%ld",index]];
    }
    
    self.date = hourlyForecast.date;
    self.tmp = hourlyForecast.tmp;
    self.windSpeedLabel.text = [NSString stringWithFormat:@"风速%@Km/h",hourlyForecast.spd];
    self.windDirAndScLabel.text = [NSString stringWithFormat:@"%@ %@",hourlyForecast.dir,hourlyForecast.sc];
    self.flLabelView.rightLabel.text = [NSString stringWithFormat:@"%@°",hourlyForecast.tmp];
    self.hunLabelView.rightLabel.text = [NSString stringWithFormat:@"%@%%",hourlyForecast.hum];
//    NSLog(@"hum====%@",hourlyForecast);
    self.visLabelView.rightLabel.text = [NSString stringWithFormat:@"%@km",weather.vis];

}

@end
